function [medianSY, windowsCount] = nn(filteredData, W1, W2, B1, B2, samplingRate)

% validate arguments
failIfFalse((samplingRate == 256) || (samplingRate == 512), 'Unexpected sampling rate');
failIfFalse(isvector(filteredData), 'filteredData should be a n x 1 vector');

% constants
NN_INPUT_FREQ = 128;
MIN_SAMPLING_FREQ = 256;

% Subsampling factor
M = samplingRate/MIN_SAMPLING_FREQ;

WINDOW_SIZE = NN_INPUT_FREQ/2;
HALF_WINDOW_SIZE = WINDOW_SIZE/2;

% Add a quarter of a second zero samples, before and after data.
% This is done for sliding window handling...
filteredData = [zeros(1, M * WINDOW_SIZE), filteredData'];
filteredData = [filteredData, zeros(1, M * WINDOW_SIZE)];

% Subsampling...
filteredData = filteredData(1 : 2 * M : length(filteredData))';

% These variables will hold the nn output
% Add a quarter of second zero samples for sliding window handling...
SY = [];
SY = [SY, ones(1, HALF_WINDOW_SIZE)];

% SN = []; 
% SN = [SN, ones(1, HALF_WINDOW_SIZE)];

windowsCount = calcSlidingWindows(WINDOW_SIZE, 1, length(filteredData));
for i = 2 : windowsCount 
	window = filteredData(i : ((WINDOW_SIZE - 1) + i));
	
	% Simulate a neural network - change this for Matlab 7!
	nntwarn OFF
	Bpout = simuff(window, W1, B1, 'logsig', W2, B2, 'logsig'); % Backpropagation output
	SY(i + (HALF_WINDOW_SIZE - 1)) = Bpout(1);
    
% 	SN(i + (HALF_WINDOW_SIZE - 1)) = Bpout(2);
end

% Again sliding window handling...
SY = prepareForSliding(SY, HALF_WINDOW_SIZE);
% SN = prepareForSliding(SN, HALF_WINDOW_SIZE);

% pass the results through a median filter
medianSY = [];
for i = 2 : windowsCount
	window1 = SY(i : ((WINDOW_SIZE - 1) + i));
% 	window2 = SN(i : ((WINDOW_SIZE - 1) + i));	
	
	medianSY(i-1) = median(window1);
% 	medianSN(i-1) = median(window2);
end

%==========================================================================
function Y = prepareForSliding(X, HALF_WINDOW_SIZE)
size = length(X);
X  = [X, X(size) * ones(1, HALF_WINDOW_SIZE)];  
X(1 : HALF_WINDOW_SIZE) = X(HALF_WINDOW_SIZE + 1) * ones(1, HALF_WINDOW_SIZE);
Y = X;





